package net.zoneland.geological.project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import net.zoneland.geological.station.entity.Tbl_station_image;
import net.zoneland.geological.station.entity.Tbl_station_info;
import net.zoneland.geological.station.service.Tbl_station_imageServiceI;
import net.zoneland.geological.station.service.Tbl_station_infoServiceI;
import net.zoneland.greenleaf.utils.FileUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by cmcc on 2016/12/5.
 */
@Controller
@RequestMapping(value = "upload")
public class UploadController {

	@Autowired
	private Tbl_station_imageServiceI tbl_station_imageService;
	
	@Autowired
	private Tbl_station_infoServiceI tbl_station_infoService;

    @RequestMapping(value="uploadFile",method= RequestMethod.POST,produces="text/html;charset=utf-8")
    @ResponseBody
    public String uploadFile(@RequestParam("pic")CommonsMultipartFile pic,HttpServletRequest req,HttpServletResponse response) throws Exception {
        //设置文件保存的本地路径
//        String filePath = req.getSession().getServletContext().getRealPath("/uploadFiles/");
        String filePath = "/Users/cmcc/Pictures/test";
        filePath = "D:\\upload";

        String fileName = pic.getOriginalFilename();
        String fileType = fileName.split("[.]")[1];
        //为了避免文件名重复，在文件名前加UUID
        String uuid = UUID.randomUUID().toString().replace("-","");
        String uuidFileName = uuid + fileName;
        File f = new File(filePath+"/"+uuid+"."+fileType);
        //将文件保存到服务器
        FileUtil.upFile(pic.getInputStream(), uuidFileName, filePath);

        String stationId = req.getParameter("stationId");
        Map<String, Object> map = new HashMap<>();
        map.put("id", stationId);
        Tbl_station_info station_info = tbl_station_infoService.get(map);
        
        //保存在数据库中
        Tbl_station_image station_image = new Tbl_station_image();
        station_image.setArea_code(station_info.getArea_code());
        station_image.setStation_id(stationId);
        station_image.setStation_name(station_info.getStation_name());
        station_image.setImage_url(uuidFileName);
        tbl_station_imageService.add(station_image);

        return uuidFileName;

    }

    @RequestMapping("/toUpload")
    public String toUpload(@RequestParam Integer stationId, HttpServletRequest request) {

        request.setAttribute("stationId", stationId);
        return "/demos/station/station_image_upload";
    }

    @ResponseBody
    @RequestMapping(value = "uploadfile")
    public Map uploadFile(@RequestParam(value = "upfile", required = false) MultipartFile[] upfile) throws Exception{
        Map<String,String> map = new HashMap<String, String>();

        String filePath = "/Users/cmcc/Pictures/test";
        filePath = "D:\\upload";

        if (upfile != null && upfile.length > 0){
            //循环获取file数组中得文件
            for(int i = 0;i<upfile.length;i++){
                MultipartFile file = upfile[i];

                String fileName = file.getOriginalFilename();
                String fileType = fileName.split("[.]")[1];
                //为了避免文件名重复，在文件名前加UUID
                String uuid = UUID.randomUUID().toString().replace("-","");
                String uuidFileName = uuid + fileName;
                File f = new File(filePath+"/"+uuid+"."+fileType);
                //将文件保存到服务器
                FileUtil.upFile(file.getInputStream(), uuidFileName, filePath);
                //返回对象
                System.out.println("上传文件"+fileName);
                try {
//                    new QiniuUtils().uploadFile(fileByte,fileName);
                    map.put("url",uuidFileName);
                    map.put("name",uuidFileName);
                    map.put("state","SUCCESS");
                }catch (Exception e){
                    e.printStackTrace();
                    map.put("state","上传失败!");
                }
            }
        }
        return map;
    }
}
